import logging
import time

from ...constants.common_sequence import SWITCH_ENV
from appium.webdriver.common.appiumby import AppiumBy
from selenium.webdriver.chrome.webdriver import WebDriver


from ...page.pay_page.pay_out_page import PayOutPage



class AppendFromPreOrderPage(object):
    """
    Description:桌台选中加载已点后的订单页
    """

    def __init__(self, client):
        self.client = client
        self.driver: WebDriver = client.driver

    """
    Service Function
    """
    def time_sleep(self,seconds:int):
        """
        睡眠
        Params:
        -seconds:秒
        """
        time.sleep(seconds)
        return self

    """
    Button Function
    """

    def increase_return_quantity(self, quality: int):
        """
        增加退菜数量
        Params:
        -quality:数量
        """
        element = self.driver.find_element(AppiumBy.ID, f'{SWITCH_ENV}:id/tv_add')
        for record in range(quality):
            element.click()

    def decrease_return_quantity(self, click: int):
        """
        减少退菜数量
        """
        element = self.driver.find_element(AppiumBy.ID, f'{SWITCH_ENV}:id/tv_sub')
        for record in range(click):
            element.click()

    def add_meal(self, goods_list):
        """
        加单菜品
        Params:
        -goods_list:菜品列表
        """
        self.driver.find_element(AppiumBy.ID, f'{SWITCH_ENV}:id/tv_table_add').click()
        from cash_pos.page.order_page.order_page import OrderPage
        OrderPage(self.client).goods_add_to_cart(goods_list)
        return self

    def return_meal(self, name, quality):
        """
        退菜（现在的）
        Params:
        -name:菜品名称
        -quality:退菜数量
        """

        index_elements = self.driver.find_elements(AppiumBy.ID, f'{SWITCH_ENV}:id/tv_cart_index')
        # 找到所有名字为此的货物的父级标签
        parent_list = self.driver.find_elements(AppiumBy.XPATH,
                                                f'//android.widget.TextView[@resource-id="{SWITCH_ENV}:id/tv_goods""]/parent::')
        available_return = {}
        for index, parent in enumerate(parent_list):
            good_test = self.driver.find_element(AppiumBy.XPATH,
                                                 f'./*[@resource-id="{SWITCH_ENV}:id/tv_goods"]').text
            if good_test == name:
                child_list = parent.find_elements(AppiumBy.XPATH,
                                                  "./child::*")
                child_list_return = parent.find_element(AppiumBy.XPATH,
                                                        f"./child::* and ./*[@resource-id='{SWITCH_ENV}:id/iv_return']")
                if child_list_return:
                    available_return[index] = (child_list, child_list_return)
        # 校验参数
        count = 0
        for item in available_return:
            count += int(index_elements[int(item)].text)
        if count <= quality:
            logging.error(f"{name}退菜可用的数量为{str(count)},不能进行合理退菜！")
        # 进行退菜
        sum_return = 0
        for item in available_return:
            count = int(index_elements[int(item)].text)
            if count < quality:
                self.decrease_return_quantity(click=count)
                sum_return += count
        self.driver.find_element(AppiumBy.ID, f'{SWITCH_ENV}:id/rl_num_submit').click()

    def clean_table(self):
        """
        清空桌台
        """
        self.driver.find_element(AppiumBy.ID, f'{SWITCH_ENV}:id/fl_table_clear').click()
        self.time_sleep(2)
        self.driver.find_element(AppiumBy.ID, f'{SWITCH_ENV}:id/tv_sure').click()
        return self

    def forward_to_pay(self):
        """
        去支付
        """
        self.driver.find_element(AppiumBy.XPATH,f'//android.widget.TextView[@resource-id="{SWITCH_ENV}:id/tv_cart_cashier"]').click()
        return PayOutPage(self.client)

    def turn_to_activity(self):
        """
        切换至活动页
        """
        self.driver.find_element(AppiumBy.ID,f'{SWITCH_ENV}:id/rl_activity_list').click()
        from cash_pos.page.security_page.activity_page import ActivityPage
        return ActivityPage(self.client)

